package org.skydiveapp;

import java.util.ArrayList;

import org.skydiveapplibrary.repository.LogEntryRepository;
import org.skydiveapplibrary.ui.LogEntryListCursorAdapter;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

public class LogEntrySelectListActivity extends BaseListActivity
{
	// repository
	private LogEntryRepository repository = new LogEntryRepository(this);
	
	@Override
    public void onCreate(Bundle savedInstanceState)
    { 
    	super.onCreate(savedInstanceState);
    	
    	// init view
    	getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    	setHeaderText(R.string.log_entry_list_select_label);
    	
    	// Display footer buttons
    	setLeftButtonVisibility(View.VISIBLE);
    	setRightButtonVisibility(View.VISIBLE);
    }
	
	@Override
    public void onResume()
    {
    	super.onResume();
    	
    	loadData();
    }
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent)
	{
		setResult(RESULT_OK);
		finish();
	}
	
	private void loadData()
    {
    	// set adapter
    	Cursor cursor = repository.getLogEntryCursor();
    	startManagingCursor(cursor);
    	LogEntryListCursorAdapter logEntryListCursorAdapter = new LogEntryListCursorAdapter(this,cursor, true);
		setListAdapter(logEntryListCursorAdapter);
    }
	
	@Override
    protected void onLeftImageButtonClick()
    {
		setResult(RESULT_CANCELED);
		finish();
    }
    
    @Override
    protected void onRightImageButtonClick()
    {
    	// get selected ids
    	ArrayList<Integer> logEntryIds = ((LogEntryListCursorAdapter)getListAdapter()).getSelection();
    	
    	// start signature activity
    	Intent intent =  new Intent(this, SignatureActivity.class);
		intent.putExtra(Constants.EXTRA_SELECTED_LOG_ENTRY_IDS, logEntryIds);
		startActivityForResult(intent, 0);
    }
    
    @Override
    protected void onListItemClick(ListView listView, View view, int indx, long id)
    {
    	// update selection
    	((LogEntryListCursorAdapter)getListAdapter()).toggleSelection((int)id);
    }
}